package com.kbbywyclient.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.BankCardInfoModule;
import com.kbbywyclient.presenter.BankCardInfoPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.BankCardInfoContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.CheckFormat;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomToolBar;

import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by Jiangzx on 2017/9/8 10:11.
 * 填银行卡信息
 */

public class BankCardInfoActivity extends BaseActivity implements BankCardInfoContract.View{
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.tv_card_type)
    TextView tvCardType;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.bt_next_stop)
    Button btNextStep;
    @BindView(R.id.checkbox)
    CheckBox checkBox;

    private String name;//持卡人姓名
    private String cardNumber;//银行卡卡号
    private String cardCode;//银行编码
    private String idCardNumber;//身份证号码
    private String cardName;//银行名字

    private String phoneNumber;//手机号码

    private int cardType;//卡片类型 1储蓄卡 2信用卡

    @Inject
    BankCardInfoPresenter presenter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_bank_card_info;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->handleGoBack());
    }

    @Override
    public void initData() {
        Intent intent=getIntent();
        name=intent.getStringExtra(VerifyPhoneActivity.NAME_S);
        cardNumber=intent.getStringExtra(VerifyPhoneActivity.CARD_NUMBER_S);
        cardCode=intent.getStringExtra(VerifyPhoneActivity.CARD_CODE_S);
        idCardNumber=intent.getStringExtra(VerifyPhoneActivity.ID_CARD_NUMBER_S);
        cardName=intent.getStringExtra(VerifyPhoneActivity.BANK_NAME_S);
        cardType=intent.getIntExtra(VerifyPhoneActivity.CARD_TYPE_I,1);
        tvCardType.setText(cardName+" "+(cardType==1?"储蓄卡":"信用卡"));
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getBankCardInfoComponent(new BankCardInfoModule(this)).inject(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("银行卡绑定-填银行卡信息");
    }

    @OnTextChanged(R.id.et_phone)
    public void afterTextChange(Editable s){
        String phone=s.toString().trim();
        if(phone.length()==11&&checkBox.isChecked()){
            btNextStep.setEnabled(true);
        }else{
            btNextStep.setEnabled(false);
        }
    }

    @OnClick({R.id.bt_next_stop,R.id.tv_agreement})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.bt_next_stop:
                String phone=etPhone.getText().toString().trim();
                if(CheckFormat.isMobile(phone)){
                    phoneNumber=phone;
                    presenter.checkBankInfo(getUser(this).getSalesID(),
                            name,
                            cardNumber,
                            idCardNumber,phone);
                }else{
                    ToastUtil.showShort("请输入正确的手机号码");
                }
                break;
            case R.id.tv_agreement:
                //用户协议
                HashMap<String,Object> webParams=new HashMap<>();
                webParams.put(WebViewActivity.KEY_TITLE_S,"用户协议");
                webParams.put(WebViewActivity.KEY_URL_S, API.USER_AGREEMENT);
                ActivitySkipUtil.skipAnotherActivity(this,
                        WebViewActivity.class,
                        webParams);
                break;
        }
    }

    @OnCheckedChanged(R.id.checkbox)
    public void onCheckedChanged(CompoundButton bt,boolean checked){
        if(!checked){
            btNextStep.setEnabled(false);
        }else{
            if(etPhone.getText().toString().trim().length()>0){
                btNextStep.setEnabled(true);
            }
        }
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void checkBankInfoSuccess(BaseModel model) {
        //银行卡4要素验证成功
        HashMap<String, Object> params = new HashMap<>();
        params.put(VerifyPhoneActivity.NAME_S, name);
        params.put(VerifyPhoneActivity.ID_CARD_NUMBER_S, idCardNumber);
        params.put(VerifyPhoneActivity.CARD_NUMBER_S, cardNumber);
        params.put(VerifyPhoneActivity.CARD_CODE_S,cardCode);
        params.put(VerifyPhoneActivity.BANK_NAME_S, cardName);
        params.put(VerifyPhoneActivity.CARD_TYPE_I, cardType);
        params.put(VerifyPhoneActivity.BANK_MOBILE_S, phoneNumber);
        ActivitySkipUtil.skipAnotherActivity(BankCardInfoActivity.this,
                VerifyPhoneActivity.class, params);
        finish();
    }

    CustomDialog errorDialog;
    @Override
    public void checkBankInfoError() {
        errorDialog=new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "信息校验错误，请填写本人真实信息", new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                errorDialog.dismiss();
            }

            @Override
            public void right() {
                errorDialog.dismiss();
                HashMap<String,Object> params=new HashMap<>();
                params.put(VerifyPhoneActivity.NAME_S,name);
                params.put(VerifyPhoneActivity.CARD_NUMBER_S,cardNumber);
                params.put(VerifyPhoneActivity.ID_CARD_NUMBER_S,idCardNumber);
                ActivitySkipUtil.skipAnotherActivity(BankCardInfoActivity.this,
                        CardHolderInfoActivity.class,params);
                finish();
            }
        },"取消","返回修改");
        errorDialog.show();
    }

    CustomDialog cancelDialog;
    private void handleGoBack(){
        if(cancelDialog==null){
            cancelDialog=new CustomDialog(BankCardInfoActivity.this,
                    R.style.Customdialog, "卡贝贝提示", "是否取消绑卡？", new CustomDialog.ButtonListener() {
                @Override
                public void left() {
                    cancelDialog.dismiss();
                    finish();
                }

                @Override
                public void right() {
                    cancelDialog.dismiss();
                }
            },"是","否");
        }
        if(!cancelDialog.isShowing()){
            cancelDialog.show();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            handleGoBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
